﻿using Pipes.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Pipes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for DelimitedTextLineReaderTest and is intended
    ///to contain all DelimitedTextLineReaderTest Unit Tests
    ///</summary>
  [TestClass()]
  public class DelimitedTextLineReaderTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for GetEnumerator
    ///</summary>
    [TestMethod()]
    public void SimpleDelimLineTest()
    {
      var input = "\"abc\",123,\r\n\"first, \r\n\"\" second\",abc\"";
      var expected = new List<string[]>() { new string[] { "abc", "123", "" }, new string[] { "first, \r\n\" second", "abc\"" } };
      var reader = new IO.StringTextSource(input).Pipe(new DelimitedTextLineReader());
      reader.FieldEnclosingChar = '"';
      reader.EnclosingCharEscape = '"';
      reader.AddDelim(',');

      var i = 0;
      foreach (var line in reader)
      {
        CollectionAssert.AreEqual(expected[i], line.ToArray());
        i++;
      }
    }

    [TestMethod()]
    public void DelimLineBlanks()
    {
      var input = "abc\r\n123\r\n\r\nanother\r\n";
      var expected = new List<string[]>() { new string[] { "abc" }, new string[] { "123" }, new string[] { }, new string[] { "another" }, new string[] { } };
      var reader = new IO.StringTextSource(input).Pipe(new DelimitedTextLineReader());
      reader.FieldEnclosingChar = '"';
      reader.EnclosingCharEscape = '"';
      reader.AddDelim(',');

      var i = 0;
      foreach (var line in reader)
      {
        CollectionAssert.AreEqual(expected[i], line.ToArray());
        i++;
      }
    }

    [TestMethod()]
    public void DelimLine_SkipBlanks()
    {
      var input = "abc\r\n123\r\n\r\nanother\r\n";
      var expected = new List<string[]>() { new string[] { "abc" }, new string[] { "123" }, new string[] { "another" }};
      var reader = new IO.StringTextSource(input).Pipe(new DelimitedTextLineReader());
      reader.FieldEnclosingChar = '"';
      reader.EnclosingCharEscape = '"';
      reader.AddDelim(',');
      reader.SkipEmptyLines = true;

      var i = 0;
      foreach (var line in reader)
      {
        CollectionAssert.AreEqual(expected[i], line.ToArray());
        i++;
      }
    }
  }
}
